package com.yixin.hospital.controller;

import com.yixin.hospital.common.ApiResult;
import com.yixin.hospital.common.Constant;
import com.yixin.hospital.entity.dto.req.drugdepositoryAuth.DrugDepositoryAuthReqDTO;
import com.yixin.hospital.entity.vo.DrugDepositoryAuthVo;
import com.yixin.hospital.service.DrugDepositoryDutyAuthService;
import com.yixin.hospital.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/drug-depository-auth")
@Api(value = "药品仓库权限管理", tags = "药品仓库权限管理")
@Slf4j
public class DrugDepositoryAuthController {

    @Resource
    private DrugDepositoryDutyAuthService drugDepositoryDutyAuthService;

    @GetMapping("/list")
    @ApiOperation(value = "药品仓库权限列表")
    public ApiResult<List<DrugDepositoryAuthVo>> list(HttpServletRequest request) {
        Long userId = JwtUtil.getUserId(request.getHeader(Constant.AUTHORIZATION));
        return drugDepositoryDutyAuthService.allList(userId);
    }

    @PostMapping("/add")
    @ApiOperation(value = "添加权限")
    public ApiResult add(@Valid @RequestBody DrugDepositoryAuthReqDTO reqDTO, HttpServletRequest request) {
        String userName = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));
        return drugDepositoryDutyAuthService.add(reqDTO, userName);
    }

    @DeleteMapping("/remove")
    @ApiOperation(value = "删除权限")
    public ApiResult remove(@Valid @RequestBody DrugDepositoryAuthReqDTO reqDTO) {
        return drugDepositoryDutyAuthService.delete(reqDTO);
    }

    /**
     * 校验库权限
     * @param userId
     * @param depositoryId
     * @param optType
     * @return
     */
    public boolean checkDrugDepositoryAuth(Long userId, Long depositoryId, Integer optType) {
        return drugDepositoryDutyAuthService.checkDrugDepositoryAuth(userId, depositoryId, optType);
    }

    /**
     * 根据权限查询库
     * @param optType
     * @return
     */
    @GetMapping("/query-depositoryIds-by-optType")
    @ApiOperation(value = "根据操作权限查库列表")
    public ApiResult<List<Long>> getDepositoryIdListByAuth(@RequestParam Integer optType, HttpServletRequest request) {
        Long userId = JwtUtil.getUserId(request.getHeader(Constant.AUTHORIZATION));
        return drugDepositoryDutyAuthService.getDepositoryIdListByAuth(optType, userId);
    }

}
